package com.xingchi.tornado.core.utils;

import java.util.Random;

/**
 * 随机数工具类
 *
 * @author xingchi
 * @date 2024/4/21 20:54
 */
public class RandomUtils {

    private static final String UPPER_LOWER_CASE_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    private static final String ALPHANUMERIC = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    /**
     * 生成随机字符串
     *
     * @param length 字符串位数
     * @return 随机字符串
     */
    public static String getRandomString(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(UPPER_LOWER_CASE_ALPHABET.length());
            sb.append(UPPER_LOWER_CASE_ALPHABET.charAt(index));
        }
        return sb.toString();
    }

    /**
     * 生成随机数字
     *
     * @return 随机数
     */
    public static Integer getRandomNumber(int length) {
        if (length <= 0) {
            length = 6;
        }
        int min = (int) Math.pow(10, length - 1);
        int max = (int) Math.pow(10, length) - 1;
        return getRandomNumber(min, max);
    }

    /**
     * 生成随机数字
     *
     * @param min 随机数字的最小值
     * @param max 随机数字的最大值
     * @return 随机数
     */
    public static Integer getRandomNumber(int min, int max) {
        Random random = new Random();
        return random.nextInt(max - min + 1) + min;
    }

    /**
     * 生成随机字符串和数字混合
     *
     * @param length 随机字符串的长度
     * @return 随机字符串
     */
    public static String getRandomStringAndNumber(int length) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(ALPHANUMERIC.length());
            sb.append(ALPHANUMERIC.charAt(index));
        }
        return sb.toString();
    }

}
